﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.IO;

namespace GeoSketch2.Core
{
    /// <summary>
    /// A box which contains the thumbnails for the picture manager
    /// </summary>
    public class ThumbnailBox
    {
        #region Fields
        /// <summary>
        /// The panel where the thumbnail is displayed
        /// </summary>
        public Panel DisplayPanel;

        /// <summary>
        /// The label where the filename of the picture is displayed
        /// </summary>
        public Label FileNameLabel;

        /// <summary>
        /// The button to delete the pictures and the parameter file *.gsp
        /// </summary>
        public Button DeleteButton;

        /// <summary>
        /// The fullname of the picture
        /// </summary>
        public string FullFileName;

        Bitmap Picture; 
        #endregion

        #region Constructor
        /// <summary>
        /// A box which contains the thumbnails for the picture manager
        /// </summary>
        public ThumbnailBox(string FileName, Size ThumbnailSize)
        {
            this.FullFileName = FileName;

            this.DisplayPanel = new Panel();
            this.FileNameLabel = new Label();
            this.DeleteButton = new Button();

            this.DisplayPanel.Size = ThumbnailSize;
            this.DisplayPanel.Paint += new PaintEventHandler(DisplayPanel_Paint);
            this.DisplayPanel.Tag = this;
            this.DisplayPanel.Controls.Add(this.DeleteButton);

            this.FileNameLabel.AutoSize = true;
            this.FileNameLabel.TextAlign = ContentAlignment.MiddleCenter;
            this.FileNameLabel.Text = Path.GetFileName(FileName);

            this.DeleteButton.AutoSize = true;
            this.DeleteButton.AutoSizeMode = AutoSizeMode.GrowAndShrink;
            this.DeleteButton.Text = "X";
            this.DeleteButton.ForeColor = Color.Red;
            this.DeleteButton.Click += new EventHandler(DeleteButton_Click);

            using (Image imgtemp = Image.FromFile(Path.GetDirectoryName(FileName) + "\\" + Path.GetFileNameWithoutExtension(FileName) + "_thumbnail.jpg"))
            {
                Picture = new Bitmap(imgtemp);
            }

        } 
        #endregion

        #region Events
        void DeleteButton_Click(object sender, EventArgs e)
        {
            DialogResult dr = MessageBox.Show("Definitely delete this file?", "File deletion", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2);
            if (dr == DialogResult.Yes)
            {
                Picture.Dispose();
                Picture = null;
                Picture = new Bitmap(5, 5);
                File.Delete(FullFileName);
                File.Delete(Path.GetDirectoryName(FullFileName) + "\\" + Path.GetFileNameWithoutExtension(FullFileName) + "_thumbnail.jpg");
                File.Delete(Path.GetDirectoryName(FullFileName) + "\\" + Path.GetFileNameWithoutExtension(FullFileName) + ".gsp");
                this.DisplayPanel.Parent.Parent.Hide();
                this.DisplayPanel.Parent.Parent.Show();
            }
        }

        void DisplayPanel_Paint(object sender, PaintEventArgs e)
        {
            e.Graphics.DrawImage(Picture, Tools.FitRectangleIntoRectangle(this.DisplayPanel.ClientRectangle, new Rectangle(0, 0, Picture.Width, Picture.Height)), new Rectangle(0, 0, Picture.Width, Picture.Height), GraphicsUnit.Pixel);
        } 
        #endregion

        #region Properties
        /// <summary>
        /// Gets or sets the location of the thumbnailbox
        /// </summary>
        public Point Location
        {
            get
            {
                return DisplayPanel.Location;
            }
            set
            {
                DisplayPanel.Location = value;
                this.FileNameLabel.Top = DisplayPanel.Bottom + Vars.Setup.StandardObjects.ThumbnailBox.SpaceBetweenDisplayBoxAndLabel;
                this.FileNameLabel.Left = DisplayPanel.Left + (DisplayPanel.Width - FileNameLabel.Width) / 2;
                this.DeleteButton.Top = DisplayPanel.Height - DeleteButton.Height;
                this.DeleteButton.Left = DisplayPanel.Width - DeleteButton.Width;
            }
        }

        /// <summary>
        /// Gets the bounding box of the thumnail
        /// </summary>
        public Rectangle Rectangle
        {
            get
            {
                return new Rectangle(Location, new Size(DisplayPanel.Width, FileNameLabel.Bottom - DisplayPanel.Top));
            }
        }

        /// <summary>
        /// Gets the filename of the picture, without the path
        /// </summary>
        public string FileName
        {
            get
            {
                return Path.GetFileName(this.FullFileName);
            }
        } 
        #endregion
    }
}
